package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.InCarBean;
import com.project.bean.InOrderBean;
import com.project.bean.StorageBean;
import com.project.mapper.IInCarMapper;
import com.project.mapper.IInDetailMapper;
import com.project.mapper.IInOrderMapper;
import com.project.mapper.IStorageMapper;
import com.project.service.IInCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;
@Service
@Transactional
public class InCarServiceImpl implements IInCarService {
    @Autowired
    private IInCarMapper carMapper;
    @Autowired
    private IStorageMapper storageMapper;
    @Autowired
    private IInOrderMapper orderMapper;
    @Autowired
    private IInDetailMapper detailMapper;
    @Override
    public void add(StorageBean storageBean, String opt,Integer userId ,Integer addNum) {
        if ("add".equals(opt)){
            storageMapper.insert(storageBean);
            InCarBean inCarBean = new InCarBean();
            inCarBean.setProductId(storageBean.getId());
            inCarBean.setUserId(userId);
            inCarBean.setNum(addNum);
            carMapper.insert(inCarBean);
        }
        if ("update".equals(opt)){
            QueryWrapper<InCarBean> qw =new QueryWrapper<>();
            qw.eq("fk_productId",storageBean.getId());
            InCarBean car = carMapper.selectOne(qw);
            if (car==null){
                InCarBean inCarBean = new InCarBean();
                inCarBean.setProductId(storageBean.getId());
                inCarBean.setUserId(userId);
                inCarBean.setNum(addNum);
                carMapper.insert(inCarBean);
            }else {
                car.setNum(car.getNum()+addNum);
                carMapper.updateById(car);
            }

        }

    }

    @Override
    public List<InCarBean> findByUserId(Integer id) {
        return carMapper.findByUserId(id);
    }

    @Override
    public void update(Integer carId, Integer newPrice, Integer newNum) {
        InCarBean carBean = carMapper.selectById(carId);
        carBean.setPrice(newPrice);
        carBean.setNum(newNum);
        carMapper.updateById(carBean);
    }

    @Override
    public void del(Integer carId) {
        carMapper.deleteById(carId);
    }

    @Override
    public void joinStorage(String supName, String person, Integer userId) {
        InOrderBean inOrderBean =new InOrderBean(supName,person);
        orderMapper.insert(inOrderBean);
        List<InCarBean> carList = carMapper.findByUserId(userId);
        detailMapper.addDetail(carList,inOrderBean.getId());
        for (InCarBean car :carList){
            StorageBean storage = storageMapper.selectById(car.getProductId());
            storage.setNum(storage.getNum()+car.getNum());
            storage.setiPrice(car.getPrice());
            storageMapper.updateById(storage);
        }
        QueryWrapper<InCarBean> qw=new QueryWrapper<>();
        qw.eq("fk_userId",userId);
        carMapper.delete(qw);

    }

    //处理：1.添加采购基本信息
    //2、根据用户id，查询采购车集合
    //3、批量添加采购明细；同时，累加库存商品数量，更新最新采购价
    //4、清空采购车；


}
